package com.control
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.view.mainStage;
	
	import flash.events.Event;
	
	import sekati.display.Canvas;
	import sekati.display.CoreSprite;
	
	public class ContentManager extends CoreSprite
	{
		private var bulkInstance:BulkLoader = new BulkLoader("main");	
		private var xml_url:String;
		public var _mainStage:mainStage=new mainStage();
		
		public function ContentManager()
		{
			super( );
			cacheAsBitmap = true;
			initEngine();
		}
		private function initEngine():void{
			loadbegin();		
		}
		/**set up which to load and add listener*/
		public function loadbegin():void{
			xml_url=Canvas.flashVars.xml_url?Canvas.flashVars.xml_url:"./xml/config.xml";						
			bulkInstance.add(xml_url, {id:"xml"});	
			bulkInstance.add("./images/embed/bg.gif");
			bulkInstance.add("./images/embed/bgmain.jpg");
			bulkInstance.addEventListener(BulkLoader.COMPLETE, onAllLoaded);
			bulkInstance.get("xml").addEventListener(BulkLoader.COMPLETE,onXmlLoaded);
			bulkInstance.addEventListener(BulkLoader.PROGRESS, onAllProgress);
			bulkInstance.addEventListener(BulkLoader.ERROR, onErrorOccur);
			bulkInstance.start();
		}
		/**Loaded on all Loaded is finished*/
		private function onAllLoaded(evt : Event):void{
			_mainStage._stageBg=bulkInstance.getBitmap("./images/embed/bg.gif");
			_mainStage._stageBgMain=bulkInstance.getBitmap("./images/embed/bgmain.jpg");
			_mainStage.drawStage();
			addChild(_mainStage);
			addChild(new awardItemManager);
		}
		/**This is when Loaded on progress*/
		private function onAllProgress(evt : BulkProgressEvent):void{
			//trace(evt.percentLoaded);			
		}
		/**This is when Xml on loaded complete*/
		private function onXmlLoaded(evt :Event):void{
			var configXml:XML=bulkInstance.getXML(xml_url);
			var font_url:String=configXml.custom.fonts_uri.toString();
			bulkInstance.add(font_url, {id:"font"});	
		}
		private function onErrorOccur(evt:Event):void{
			trace("Load error");
		}
		public function resize():void{
			_mainStage.resize();
		}

	}
}